﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

public class SSTCryptographer
{
    public static string _KEY;

	public SSTCryptographer()
	{
	}
    public static string Key
    {
        set
        {
            _KEY = value;
        }
    }
    public static string Encrypt(string strToEncrypt)
    {
        try
        {
            return Encrypt(strToEncrypt, _KEY);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    public static string Decrypt(string strEncrypted)
    {
        try
        {
            return Decrypt(strEncrypted, _KEY);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    public static string Encrypt(string strToEncrypt, string strKey)
    {
        try
        {
            TripleDESCryptoServiceProvider objDES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider objMD5 = new MD5CryptoServiceProvider();
            byte[] byteHash, byteBuff;
            string strTempKey = strKey;
            byteHash = objMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey));
            objMD5 = null;
            objDES.Key = byteHash;
            objDES.Mode = CipherMode.ECB;
            byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
            return Convert.ToBase64String(objDES.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    public static string Decrypt(string strEncrypted, string strKey)
    {
        try
        {
            TripleDESCryptoServiceProvider objDES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider ObjMD5 = new MD5CryptoServiceProvider();
            byte[] byteHash, byteBuff;
            string strTempKey = strKey;
            byteHash = ObjMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
            ObjMD5 = null;
            objDES.Key = byteHash;
            objDES.Mode = CipherMode.ECB;
            byteBuff = Convert.FromBase64String(strEncrypted);
            string strDecrypted = ASCIIEncoding.ASCII.GetString(objDES.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            objDES = null;
            return strDecrypted;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}